package 爆刷leetcode.day0614;

import java.util.Stack;

/**
 * @Author yaya
 * @Create 2024/6/14 9:32
 * Description:
 */
public class Code020_IsValid {
    public static boolean isValid(String s) {
        if (s == null || s.length() == 1){
            return false;
        }
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char c = s.charAt(i);
            if (c == '(' || c == '[' || c == '{') {
                stack.push(c);
            } else if (c == ')' || c == ']' || c == '}') {
                if (stack.isEmpty()) {
                    return false;
                }
                char rightChar = stack.pop();
                switch (c) {
                    case ')' -> {
                        if (!(rightChar == '(')) return false;
                    }
                    case ']' -> {
                        if (!(rightChar == '[')) return false;
                    }
                    case '}' -> {
                        if (!(rightChar == '{')) return false;
                    }
                }
            }
        }
        if (!stack.isEmpty()){
            return false;
        }
        return true;
    }

    public static void main(String[] args) {
        System.out.println(isValid("(]"));
        System.out.println(isValid("(("));
        System.out.println(isValid("()"));
        System.out.println(isValid(""));
        System.out.println(isValid("("));
    }
}
